// 在lectures.js或teacher.js中添加以下代码
$(document).ready(function() {
    // 委托事件处理到动态内容容器
    $('.dynamic-content').on('click', '.btn-detail', function() {

        const $row = $(this).closest('tr');
        const status = $row.data('status');
        console.log('status:', status)


        if (status === 'PENDING') {
           showToast("考勤未开始","warning")
            return false;
        }else if (status === 'CANCELED') {
            showToast("考勤已取消","warning")
            return false;
        }
        const attendanceTime = $(this).data('time').split('T')[0];
        const attendanceCourse=$(this).data('course')
        // console.log('点击了考勤详情按钮');
        // console.log('考勤时间:', attendanceTime)
        // console.log('考勤课程:', attendanceCourse)

        // 发起AJAX请求获取考勤详情
        $.ajax({
            url: 'http://localhost:9220/AttendanceSystem/GetAttendanceDetailController', // 替换为你的后端接口
            method: 'POST',
            dataType: 'json',
            crossDomain: true,
            contentType: 'application/json',
            data:JSON.stringify({
                course: attendanceCourse,
                time: attendanceTime
                // 如果需要还可以添加其他参数，例如：
                // teacherId: ${sessionScope.teacher.id}
            }),
            success: function(response) {
                // console.log('response:', response.students)
                // console.log('成功');
                // 填充模态框内容
                const modalBody = $('.attendance-modal .modal-body');
                modalBody.empty().append(`
                    <div class="attendance-detail">
                        
                        <div class="student-list">
                            <table class="detail-table">
                                <thead>
                                    <tr>
                                        <th>学号</th>
                                        <th>课程名称</th>
                                        <th>签到时间</th>
                                        <th>状态</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    ${response.students.map(student => `
                                        <tr>
                                            <td>${student.studentId}</td>
                                            <td>${student.courseName}</td>
                                            <td>${student.attendanceDate}</td>
                                            <td class="status ${student.status}">
                                               ${student.status}
                                            </td>
                                        </tr>
                                    `).join('')}
                                </tbody>
                            </table>
                        </div>
                    </div>
                `);

                // 显示模态框
                $('.attendance-modal').fadeIn();
            },
            error: function() {
                alert('获取考勤详情失败');
            }
        });
    });

    // 关闭模态框
    $('.attendance-modal .btn-close, .attendance-modal').on('click', function(e) {
        if (e.target === this) {
            $('.attendance-modal').fadeOut();
        }
    });
});
